<?php

namespace sms;

use think\facade\Config;

class EmaySms
{

    const YM_SMS_ADDR = "www.btom.cn:8080"; /*接口地址,请联系销售获取*/
    const YM_SMS_SEND_URI = "/simpleinter/sendSMS"; /*发送短信接口*/
    const YM_SMS_SEND_PER_URI = "/simpleinter/sendPersonalitySMS"; /*发送个性短信接口*/
    const YM_SMS_GETREPORT_URI = "/simpleinter/getReport"; /*获取状态报告接口*/
    const YM_SMS_GETMO_URI = "/simpleinter/getMo"; /*获取上行接口*/
    const YM_SMS_GETBALANCE_URI = "/simpleinter/getBalance"; /*获取余额接口*/

    protected $config = [
        "YM_SMS_APPID" => '', /*APPID,请联系销售或者在页面获取*/
        "YM_SMS_AESPWD" => '', /*密钥，请联系销售或者在页面获取*/
    ];
    public function __construct()
    {
        $config = Config::get('sms.');
        if (isset($config['emay'])) {
            $this->config = $config['emay'];
        }

    }
    private function httpRequest($url, $data)
    {
        $data = http_build_query($data);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output, true);
    }

    private function signmd5($appId, $secretKey, $timestamp)
    {
        return md5($appId . $secretKey . $timestamp);
    }

    public function sendSMS($mobiles, $content)
    {
        $timestamp = date("YmdHis");
        $sign = $this->signmd5($this->config['YM_SMS_APPID'], $this->config['YM_SMS_AESPWD'], $timestamp);
        // 如果您的系统环境不是UTF-8，需要转码到UTF-8。如下：从gb2312转到了UTF-8
        // $content = mb_convert_encoding( $content,"UTF-8","gb2312");
        // 另外，如果包含特殊字符，需要对内容进行urlencode
        $data = array(
            "appId" => $this->config['YM_SMS_APPID'],
            "timestamp" => $timestamp,
            "sign" => $sign,
            "mobiles" => $mobiles,
            "content" => $content,
        );
        $url = self::YM_SMS_ADDR . self::YM_SMS_SEND_URI;
        $resobj = $this->httpRequest($url, $data);

        return $resobj;

    }

    public function getReport()
    {
        $timestamp = date("YmdHis");
        $sign = $this->signmd5($this->config['YM_SMS_APPID'], $this->config['YM_SMS_AESPWD'], $timestamp);
        $data = array(
            "appId" => $this->config['YM_SMS_APPID'],
            "timestamp" => $timestamp,
            "sign" => $sign,
            "number" => "300",
        );
        $url = self::YM_SMS_ADDR . self::YM_SMS_GETREPORT_URI;
        $resobj = $this->httpRequest($url, $data);
    }

    public function getMo()
    {
        $timestamp = date("YmdHis");
        $sign = $this->signmd5($this->config['YM_SMS_APPID'], $this->config['YM_SMS_AESPWD'], $timestamp);
        $data = array(
            "appId" => $this->config['YM_SMS_APPID'],
            "timestamp" => $timestamp,
            "sign" => $sign,
            "number" => "300",
        );
        $url = self::YM_SMS_ADDR . self::YM_SMS_GETMO_URI;
        $resobj = $this->httpRequest($url, $data);
    }

    public function getBalance()
    {
        $timestamp = date("YmdHis");
        $sign = $this->signmd5($this->config['YM_SMS_APPID'], $this->config['YM_SMS_AESPWD'], $timestamp);
        $data = array(
            "appId" => $this->config['YM_SMS_APPID'],
            "timestamp" => $timestamp,
            "sign" => $sign,
        );
        $url = self::YM_SMS_ADDR . self::YM_SMS_GETBALANCE_URI;
        $resobj = $this->httpRequest($url, $data);
    }

}
